<?php


namespace  App\Models;

use App\Models\Forms\Template;
use function GuzzleHttp\Promise\queue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * This is the model class for table "send".
 *
 * @property int $id
 * @property int $title 标题
 * @property int $group_id 发送的群
 * @property string $content 发送的内容
 * @property string $send_time 发送时间
 * @property int $queue_id 队列ID
 * @property string $created_at
 * @property string $updated_at
 * @property string $user_id
 * @property string $raw_data 保存的元对象
 */
class Send extends Model implements IReadable
{
    /**
     * @inheritdoc
     */
    protected $table = 'send';
    protected $guarded = [];
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => [
                'label' =>  '标题',
                'width' => '250',
                'type' => "text"
            ],
            'status' => [
               'type' => 'text',
               'label' => '发送状态',
               'map' => [
                   "未发送",
                   '已发送'
               ]
            ],
          /*  'group_id' => [
                'type' => 'text',
                'label' => '发送的群',
                'map' => Group::getByUid()
            ],*/
            'content' => [
                'label' =>  '发送的内容',
                'width' => '250',
                'type' => "text"
            ],
            'send_time' => [
                'type' => 'text',
                'label' => '发送时间',
                'callable' => function($row) {
                    if ($row != "1970-01-01 00:00:00") {
                        return $row;
                    }
                    return "";
                }
            ],
            'created_at' => [
                'type' => 'text',
                'label' => '采集时间',
                'callable' => function($row) {
                    return date("Y-m-d H:i:s", strtotime($row));
                }
            ],
          //  'queue_id' => '队列ID',
            'in_queue' => [
                'label' => '加入队列',
                'width' => '50',
                'type' => "text",
                'map' => [
                    "否",
                    "是"
                ]
            ]
        ];
    }

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->status = 0;

    }

    public static function saveData(Template $template, $uid, $groupId)
    {
        if (!$template || is_null($template)) {
            return false;
        }
        $model = new self();
        $model->title = $template->title;
        $model->content = htmlspecialchars($template->getStringByTemplate());
        $model->group_id = $groupId;
        $model->send_time = "1970-01-01 00:00:00";
        $model->user_id = $uid;
        $model->raw_data = $template->toJson();
        $model->images = \GuzzleHttp\json_encode($template->getImages());
        $model->save();
        return true;
    }

    public static function addQueue($messageId)
    {
        $message = self::query()->where('id', $messageId)->first();
        if ($message->in_queue == 1) {
            return true;
        }
        DB::transaction(function() use($message){
            $queue = new Queue();
            $queue->is_read = 0;
            $queue->message_id = $message->id;
            $queue->type = Queue::send_type_group;
            $queue->user_id = $message->user_id;
            $queue->save();
            $message->in_queue = 1;
            $message->queue_id = $queue->id;
            $message->save();
            return  $queue->save();
        });
        return false;
    }



}
